package com.mt.mtbletools;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.SparseArray;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
import java.util.HashMap;
import java.util.Map;
import com.mt.mtbletools.R;
import com.mt.sdk.ble.base.BLEBase;

public class MainActivity extends FragmentActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		if (android.os.Build.VERSION.SDK_INT < 18) {

			Toast.makeText(getApplicationContext(), "你out了，系统竟然还没有到android 4.3", Toast.LENGTH_LONG).show();

			return;
		}
		
		initView();

		initeFragment();

		scandevice_btn.setChecked(true); // 设置默认的页面
	}

	// 初始化控件
	private RadioGroup menu_select_radiogrounp;
	private RadioButton scandevice_btn;
	private RadioButton function_btn;
	private RadioButton about_btn;

	private void initView() {
		menu_select_radiogrounp = (RadioGroup) findViewById(R.id.menu_select_radiogrounp);
		scandevice_btn = (RadioButton) findViewById(R.id.scandevice_btn);
		function_btn = (RadioButton) findViewById(R.id.function_btn);
		about_btn = (RadioButton) findViewById(R.id.about_btn);

		menu_select_radiogrounp.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {

				if (scandevice_btn.getId() == checkedId) {
					gotoFragment(mScanFragment);
					setTabTextColor(scandevice_btn);
					return;
				}

				if (function_btn.getId() == checkedId) {
					gotoFragment(mFuntionFragment);
					setTabTextColor(function_btn);
					return;
				}

				if (about_btn.getId() == checkedId) {
					gotoFragment(mAboutFragment);
					setTabTextColor(about_btn);
					return;
				}
			}
		});
	}
	
	// 初始化Fragment
	private FragmentManager fm;
	private ScanFragment mScanFragment;
	private FuntionFragment mFuntionFragment;
	private AboutFragment mAboutFragment;

	private void initeFragment() {
		fm = getSupportFragmentManager();
		mScanFragment = new ScanFragment();
		mFuntionFragment = new FuntionFragment();
		mAboutFragment = new AboutFragment(this);
	}

	// 跳转到另一个fragment
	private void gotoFragment(Fragment mfragment) {
		FragmentTransaction ft = fm.beginTransaction();
		ft.replace(R.id.main_page_continer, mfragment);
		ft.commit();
	}

	// 改变tab字体颜色
	private void setTabTextColor(RadioButton redio_btn) {
		scandevice_btn.setTextColor(getResources().getColor(R.color.tab_text_unclick));
		function_btn.setTextColor(getResources().getColor(R.color.tab_text_unclick));
		about_btn.setTextColor(getResources().getColor(R.color.tab_text_unclick));

		redio_btn.setTextColor(getResources().getColor(R.color.tab_text_click));
	}

}
